﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data;
using System.Data.OleDb;
using System.IO;
using webapp4U;
using webapp4U.BOL;


public partial class Admin_Photos_aspx : System.Web.UI.Page 
{
    private int CateID = -1;
    private int FileID = -1;

    protected void Page_Load(object sender, EventArgs e)
    {
        CateID = Convert.ToInt32(Request.QueryString["CateID"]);
        if (CateID > 0)
        {
            PhotoCategory obj = PhotoCategory.GetCateByCateID(CateID);
            lblAlbumsName.Text = obj.CateName;
        }

        FileID = Globals.ConvertToInt32(Globals.GetIntFromQueryString("FileID"));
        if(!Page.IsPostBack)
            Load_FileByID(FileID);
    }


    protected void Load_FileByID(int FileID)
    {
        if (FileID > 0)
        {
            PhotoFile.Visible = false;
            fqfPhoto.Enabled = false;
            webapp4U.BOL.File obj = webapp4U.BOL.File.GetFileByFileID(FileID);
            HPhotoFile.Value = obj.FileName.Trim();
            PhotoTitle.Text = obj.Title.Trim();
            txtDesc.Text = obj.Description.Trim();
            ImageFile.ImageUrl = Globals.ApplicationPath + "Photos/Image.ashx?Size=M&ImageUrl=" + obj.FileName.Trim();
        }
    }


    protected void btDelete_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridViewItem.Rows.Count; i++)
        {
            GridViewRow row = GridViewItem.Rows[i];
            bool isChecked = ((CheckBox)row.FindControl("chkSelect")).Checked;
            if (isChecked)
            {
                int ID = Convert.ToInt32(((CheckBox)row.FindControl("chkSelect")).ToolTip);
                webapp4U.BOL.File.Delete(ID);
            }
        }

        GridViewItem.DataBind();
    }


    protected string Photo_Upload()
    {
        string strFileName = "";
        // kiem tra xem user co chon hinh dai dien ko
        if (PhotoFile.HasFile)
        {
            // kiem tra phan mo rong cua file co hop le hay khong
            string strExtension = System.IO.Path.GetExtension(PhotoFile.FileName).ToUpper();
            if (strExtension == ".JPG" || strExtension == ".GIF" || strExtension == ".JPEG" || strExtension == ".PNG" || strExtension == ".BMP")
            {
                // lay ten file de khong bi trung file + phan mo rong cua file can upload
                strFileName = Common.GetFileName() + System.IO.Path.GetExtension(PhotoFile.FileName);

                //duong dan tao save file Large
                string strFilePathLarge = Request.PhysicalApplicationPath + "/UserUpload/Gallery/" + strFileName;

                //thuc hien save file Large len server
                EnumUpload uploadLarge = FileProcess.UpLoadImage(PhotoFile, strFilePathLarge, 400);
            }
            else
            {
                lblMsg.Text = "File bạn chọn upload không hợp lệ.";
                return strFileName = "";
            }
        }
        else
        {
            return HPhotoFile.Value.Trim();
        }

        return strFileName;
    }



    protected void PhotoAdd_Click(object sender, EventArgs e)
    {
        // thuc hien upload file len Server
        string FileName = Photo_Upload();

        if (FileName != "" && FileName != null)
        {
            // them du lieu vao database
            PhotoAdd(CateID, PhotoTitle.Text.Trim(), FileName);
            Response.Redirect("Photos.aspx?CateID=" + CateID);
        }
    }

   
    //ghi file vao database
    protected void PhotoAdd(int CateID, string Title, string FileName)
    {
        webapp4U.BOL.File obj = new webapp4U.BOL.File();
        obj.FileID = FileID;
        obj.CateID = CateID;
        obj.Title = Title;
        obj.FileName = FileName;
        obj.Description = txtDesc.Text.Replace(Environment.NewLine,"<br>");
        obj.FileSize = 0;
        obj.Height = 1;
        obj.Width = 1;
        obj.PostDate = DateTime.Now;
        obj.Username = "";
        obj.TotalViews = 0;
        webapp4U.BOL.File.Insert(obj);
    }

}